﻿using System;
using System.Windows.Forms;
using SmartDeviceProject1.Data;
using SmartDeviceProject1.Forms;

namespace SmartDeviceProject1.Forms
{
    public partial class AccountFormBase : Form
    {
        private CategoryType categoryType;
        public AccountFormBase(CategoryType cat)
        {
            categoryType = cat;
            InitializeComponent();
        }

        private void menuSave_Click(object sender, EventArgs e)
        {
            DBHelper dbHelper = new DBHelper();
            if( string.IsNullOrEmpty(tbDesc.Text) )
            {
                MessageBox.Show("Please Input desciption:");
                tbDesc.Focus();
                return;
            }
            decimal amount;
            try
            {
                amount = Convert.ToDecimal(tbAmount.Text);
            }
            catch(FormatException)
            {
                MessageBox.Show("Please Input Amount:");
                tbAmount.Focus();
                return;
            }
            if( amount<=0 )
            {
                MessageBox.Show("Amount Number should be greater than 0.");
                tbAmount.Focus();
                return;
            }
            Guid categoryId = new Guid(cbCategory.SelectedValue.ToString());
            dbHelper.InsertPayDetail(tbDesc.Text, "", amount,dtDate.Value,categoryId);
            // Clear last inputs
            tbDesc.Text = "";
            tbAmount.Text = "";
            MessageBox.Show("Inserted.");
        }

        private void AccountFormBase_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'mobileWalletDBDataSet.PayCategory' table. You can move, or remove it, as needed.
            payCategoryTableAdapter.AlertQuery(categoryType);
            payCategoryTableAdapter.Fill(mobileWalletDBDataSet.PayCategory);
            cbType.SelectedItem = cbType.Items[(byte) categoryType];
        }

        private void cbType_SelectedIndexChanged(object sender, EventArgs e)
        {
            categoryType = cbType.SelectedItem == cbType.Items[0] ? CategoryType.Earn : CategoryType.Spend;
            payCategoryTableAdapter.AlertQuery(categoryType);
            payCategoryTableAdapter.Fill(mobileWalletDBDataSet.PayCategory);
        }
    }
}